//
//  QCWorkController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/25.
//

import UIKit

class QCWorkController: QCViewController {
    var array = [["ACC模式"],["定时待机"],["5","10","15","20","30","60"]]
    var titles = ["ACC信号控制系统是否进入休眠状态。","休眠状态下，打气泵停止工作，控制面板息屏。","旋转表盘即可解除休眠。"]
    var values = [[false],[false],[false,false,false,false,false,false]]
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("工作模式")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSettingCell.nib(), forCellReuseIdentifier: QCSettingCell.ID())
        tableView.register(QCProtectFooter.nib(), forHeaderFooterViewReuseIdentifier:QCProtectFooter.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    override var base_info: [UInt8]{
        didSet{
            let data = [UInt8](base_info[AI_index..<base_info.count])
            if recived == false && values[0][0] != (Int(data[23]) == 0){
                return
            }
            values[0][0] = data[23] == 0
            values[1][0] = data[23] == 1 ///0=ACC模式，1=定时模式
            tableView.reloadData()
        }
    }
    
    override var bytes: [UInt8] {
        didSet{
            let ack = bytes[7]
            if ack != UInt8(AIQC.userInfo.ack){return}
            let data = [UInt8](bytes[AI_index..<bytes.count])
            if recived == false && values[0][0] != (Int(data[91]) == 0){
                recived = true
                return
            }
            values[0][0] = Int(data[91]) == 0
            values[1][0] = Int(data[91]) == 1
            values[2] = array[2].map{$0.intValue() == data[1]}
            tableView.reloadData()
        }
    }
    
    func action(_ text:String, _ index:IndexPath, _ value:Any?=nil){
        recived = false
        switch text{
        case "ACC模式",
            "定时待机":///0:ACC模式1:定时模式
            let ison = value as! Bool
            values[0][0] = index.section == 0 ? ison : !ison
            values[1][0] = !values[0][0]
            BLEData.appSetDataToPer(AIQC.setACCMode.cmd, values[0][0] ? "00" : "01").write
        default:
            values[2] = [false,false,false,false,false,false]
            values[2][index.row] = true
            let str = array[index.section][index.row]
            BLEData.appSetDataToPer(AIQC.setACCSleep.cmd, str.toHex2()).write
        }
        tableView.reloadData()
    }
    
}
//MARK: - UITableViewDataSource
extension QCWorkController:UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        if (values[0][0] == true || values[1][0] == false){return array.count - 1}
        return array.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCSettingCell.ID()) as! QCSettingCell
        cell.title.text = LS(array[indexPath.section][indexPath.row]) + (indexPath.section < array.count - 1 ? "" : "分钟")
        if indexPath.section < array.count - 1{
            cell.type = .switchBtn(values[indexPath.section][indexPath.row])
        }else{
            cell.type = .subImg(values[indexPath.section][indexPath.row] ? "选择":"未选择")
        }
        cell.block = {[weak self] ison in
            self?.action((self?.array[indexPath.section][indexPath.row]) ?? "", indexPath, ison)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCWorkController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section < array.count - 1{return}
        action(array[indexPath.section][indexPath.row], indexPath)
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section < array.count - 1 || (values[0][0] == false && values[1][0] == true) {return 40}
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section < array.count - 1 || (values[0][0] == false && values[1][0] == true){
            let view = UIView()
            view.backgroundColor = .clear
            let lbl = UILabel()
            lbl.text = LS(titles[section])
            lbl.font = UIFont.systemFont(ofSize: 14)
            lbl.textHexColor = "#AEAEAE"
            view.addSubview(lbl)
            lbl.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview().offset(20)
            }
            return view
        }
        return nil
    }
}
